 Reloader = {
    busy: false,
    interval: null,
    script: null,
    host: null,
    lastRequest: 0,
    init: function(){
	var self = this;
	this.host = 'http://127.0.0.1:8181/check/';
	this.interval = setInterval(function(){
	    try{
		if(!self.busy){
		    self.lastRequest = new Date().valueOf();
		    self.busy = true;
		    self.sendRequest();
		}else if(new Date().valueOf() - self.lastRequest > 5){
		    self.busy = false;
		}
	    }catch(er){}
	},500);
    },
    sendRequest: function(){
	if(document.body){
	    this.script = document.createElement('script');
	    this.script.type = 'text/javascript';
		this.script.async = true;
	    this.script.src = this.host + '?cacheBuster=' + new Date().valueOf().toString();
	    document.body.appendChild(this.script);
	}
    },
    callback: function(data){
	this.busy = false;
	this.reload(data);
	document.body.removeChild(this.script);
    },
    reload: function(type){
	if(type === 'css'){ this.updateCss();}
	else if(type === 'refresh'){ window.location.reload(true); }
    },
    updateCss: function(){
	var a = document.getElementsByTagName('link');
		for(var i=0;i<a.length;i++) {
			var s = a[i];
			if(s.rel.toLowerCase().indexOf('stylesheet')>=0&&s.href) {
			var h = s.href.replace(/(\&|\\?)cacheBuster=\d*/,'');
			s.href= h + (h.indexOf('?')>=0?'&':'?') +'cacheBuster='+ (new Date().valueOf());
			}
		}
    }
}

Reloader.init();
